package pos.datos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.dominio.Lugar;

public class JDBCLugarDAO implements ILugarDAO{

	@Override
	public List ObtenerLugares() {
		
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        
        try {

            String sql = "SELECT * FROM Lugar";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            
                    Lugar temp = new Lugar();
                    temp.setIDLugar(result.getString("IDLugar"));
                    temp.setImagen1(result.getString("imagen1"));
             
                    temp.setNombre(result.getString("nombre"));
                    
                    searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
            return searchResults;
       
	
}

	@Override
	public Lugar obternerlugar(String IDlugar) {
		
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        Lugar l=new Lugar();
        ResultSet result = null;
        
        
        try {

            String sql = "SELECT * FROM Lugar WHERE (IDLugar = ?)";
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, IDlugar);
            stmt.executeQuery();
            result = stmt.executeQuery();   
            result.next();
            l.setIDLugar(result.getString("IDLugar"));
            l.setNombre(result.getString("nombre"));
            l.setImagen1(result.getString("imagen1"));
      
                    
                  
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
            
       
	
        return l;

	}
}
	
	

	

